/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.cheln.explorer.ui;

import android.view.View;

import com.direction.hold.ui.CommonActivity;
import com.direction.hold.ui.FileCursorActivity;

import cn.cheln.explorer.actions.sort.Sort;
import cn.cheln.explorer.beans.ApkInfo;
import cn.cheln.explorer.beans.ApkInfoUtil;
import cn.cheln.explorer.beans.FileInfo;
import cn.cheln.explorer.config.Config;
import cn.cheln.explorer.config.Constants;
import cn.cheln.explorer.cpnt.adapter.CountAdapter;
import cn.cheln.explorer.cpnt.adapter.CursorItemSrc;
import cn.cheln.explorer.ds.MediaArgs;
import cn.cheln.explorer.ds.database.Category;
import cn.cheln.explorer.ds.database.CursorCategory;
import cn.cheln.explorer.ds.database.MimeSrc;
import cn.cheln.explorer.ds.uri.NavigationNode;
import cn.cheln.support.ui.adapter.ItemDataSrc;
import cn.cheln.support.ui.adapter.MapAdapter.AdaptInfo;

/**
 * 
 * @author chenliang
 */
public class FileListViewHolder extends ViewHolder {

	public FileCursorActivity fca;

	public FileCursorActivity getFca() {
		return fca;
	}

	public void setFca(FileCursorActivity fca) {
		this.fca = fca;
	}

	public FileListViewHolder(View view, CommonActivity activity) {
		super(view, activity);
		fca = (FileCursorActivity) activity;
	}

	public Object resetPolicy(Category cItem, Object sContent) {
		if (fca.policy.getPolicy() == -1) {
			fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
			fca.policy.setSeq(Sort.SORT_SEQ_ASC);
			sContent = fca.policy.transfer(cItem.categoryInfo.categorySign)
					.getContent();
		}
		return sContent;
	}

	public Object resetPolicyBasedOnDisplayName(Category cItem, Object sContent) {
		if (fca.policy.getPolicy() == Sort.SORT_POLICY_DISPLAYNAME) {
			fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
			fca.policy.setSeq(Sort.SORT_SEQ_ASC);
			sContent = fca.policy.transfer(cItem.categoryInfo.categorySign)
					.getContent();
		}
		return sContent;
	}

	public Object resetPolicyBasedOnType(Category cItem, Object sContent) {
		if (fca.policy.getPolicy() == Sort.SORT_POLICY_TYPE) {
			fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
			fca.policy.setSeq(Sort.SORT_SEQ_ASC);
			sContent = activity.policy
					.transfer(cItem.categoryInfo.categorySign).getContent();
			fca.sortMenuMarkInit = fca.SortMenuInAPK;
		}
		return sContent;
	}

	@Override
	public void start() {
		if (view == null) {
			view = fca.buildList();
		}
		init();
	}

	public void init() {
		fca.setContentView(view);
		fca.initNavBar();
		// fca.contentView = view;
		fca.initListView(CountAdapter.class);
		fca.initBottomBar();
		fca.listview.setAdapter(fca.adapter);
	}

	public void dataChanged(Object... params) {
		dataChanged(false);
	}

	public void dataChanged(boolean refresh) {
		ApkInfoUtil.bindFlag = false;
		Class clazz = FileInfo.class;
		NavigationNode navigation = fca.navTool.navEntity.peek();
        CursorCategory paramCat = (CursorCategory)MimeSrc.getCategoryRepository().get(
				navigation.producingSource);
        Category cItem = new CursorCategory(paramCat);

        Sort sort = fca.policy.transfer(cItem.categoryInfo.categorySign);
		Object sContent = sort.getContent();
		CursorCategory cursorCategory = null;
		ItemDataSrc dataSrc = null;
		AdaptInfo adaptInfo = fca.getAdaptInfo(); 
		try {
			if (cItem != null && cItem instanceof CursorCategory) {
				cursorCategory = ((CursorCategory) cItem);
				if (cursorCategory.contentCursor != null) {
					if (!cursorCategory.contentCursor.isClosed()) {
						cursorCategory.contentCursor.close();
					}
					cursorCategory.contentCursor = null;
				}
				if (cItem.categoryInfo.categorySign
						.equals(Constants.CateContants.CATE_MUSIC)) {
					if (fca.policy.getPolicy() == Sort.SORT_POLICY_TITLE) {
						fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
						fca.policy.setSeq(Sort.SORT_SEQ_ASC);
						sContent = fca.policy.transfer(
								cItem.categoryInfo.categorySign).getContent();
					}
					// if (Config.isLewaRom) {
					// cursorCategory.contentCursor =
					// SQLManager.queryMusic(fca.context, sContent.toString());
					// fca.adapter.changeAdaptInfo(fca.getMusicAdaptInfo());
					// clazz = MusicInfo.class;
					// } else {

					// MirrManager
					// .lookForMediaFiles(fca.context, cursorCategory.uri,
					// "audio", sContent.toString(),MediaArgs.LISTPROJECTION);
					// fca.context.getContentResolver().query(cursorCategory.uri,
					// MediaArgs.LISTPROJECTION, null, null,
					// sContent.toString());
					// }
				} else if (cItem.categoryInfo.categorySign
						.equals(Constants.CateContants.CATE_IMAGES)) {
					if (fca.policy.getPolicy() == Sort.SORT_POLICY_ALBUM
							|| fca.policy.getPolicy() == Sort.SORT_POLICY_ARTIST) {
						fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
						fca.policy.setSeq(Sort.SORT_SEQ_ASC);
						sContent = fca.policy.transfer(
								cItem.categoryInfo.categorySign).getContent();
						fca.sortMenuMarkInit = fca.SortMenuInMusic;
					} else {
						sContent = resetPolicy(cItem, sContent);
						fca.sortMenuMarkInit = -1;
					}
                    cItem.categoryInfo.imageFilterSize = Config.image_filter_size;
                } else if (cItem.categoryInfo.categorySign
						.equals(Constants.CateContants.CATE_VIDEO)) {
					if (fca.policy.getPolicy() == Sort.SORT_POLICY_ALBUM
							|| fca.policy.getPolicy() == Sort.SORT_POLICY_ARTIST) {
						fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
						fca.policy.setSeq(Sort.SORT_SEQ_ASC);
						sContent = fca.policy.transfer(
								cItem.categoryInfo.categorySign).getContent();
						fca.sortMenuMarkInit = fca.SortMenuInMusic;
					} else {
						sContent = resetPolicy(cItem, sContent);
						fca.sortMenuMarkInit = -1;
					}

				} else if (cItem.categoryInfo.categorySign
						.equals(Constants.CateContants.CATE_DOCS)) {
					if (fca.policy.getPolicy() == Sort.SORT_POLICY_ALBUM
							|| fca.policy.getPolicy() == Sort.SORT_POLICY_ARTIST) {
						fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
						fca.policy.setSeq(Sort.SORT_SEQ_ASC);
						sContent = fca.policy.transfer(
								cItem.categoryInfo.categorySign).getContent();
						fca.sortMenuMarkInit = fca.SortMenuInMusic;
					} else {
						sContent = resetPolicyBasedOnDisplayName(cItem,
								sContent);
						sContent = resetPolicy(cItem, sContent);
						fca.sortMenuMarkInit = -1;
					}

				} else if (cItem.categoryInfo.categorySign
						.equals(Constants.CateContants.CATE_PACKAGE)) {
					if (fca.policy.getPolicy() == Sort.SORT_POLICY_ALBUM
							|| fca.policy.getPolicy() == Sort.SORT_POLICY_ARTIST) {
						fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
						fca.policy.setSeq(Sort.SORT_SEQ_ASC);
						sContent = fca.policy.transfer(
								cItem.categoryInfo.categorySign).getContent();
						fca.sortMenuMarkInit = fca.SortMenuInMusic;
					} else {
						sContent = resetPolicy(cItem, sContent);
						sContent = resetPolicyBasedOnType(cItem, sContent);
						sContent = resetPolicyBasedOnDisplayName(cItem,
								sContent);
					}
					adaptInfo = fca.getApkAdaptInfo(); 
					clazz = ApkInfo.class;
				} else if (cItem.categoryInfo.categorySign
						.equals(Constants.CateContants.CATE_THEME)) {
					if (fca.policy.getPolicy() == Sort.SORT_POLICY_ALBUM
							|| fca.policy.getPolicy() == Sort.SORT_POLICY_ARTIST) {
						fca.policy.setPolicy(Sort.SORT_POLICY_TITLE);
						fca.policy.setSeq(Sort.SORT_SEQ_ASC);
						sContent = fca.policy.transfer(
								cItem.categoryInfo.categorySign).getContent();
						fca.sortMenuMarkInit = fca.SortMenuInMusic;
					} else {
						sContent = resetPolicyBasedOnDisplayName(cItem,
								sContent);
						sContent = resetPolicy(cItem, sContent);
						fca.sortMenuMarkInit = -1;
					}

				}

				cursorCategory.contentCursor = MediaArgs.search(fca,
						cursorCategory.uri, cursorCategory.categoryInfo,
						SearchListViewHolder.selectionSearch, " order by "
								+ sContent.toString());
				fca.adapter.changeAdaptInfo(adaptInfo);
				dataSrc = new CursorItemSrc(fca.context,
						cursorCategory.contentCursor, fca.datamap, clazz);
				(fca.adapter).setItemDataSrc(dataSrc);
				fca.adapter.reinitSelectedAllBck(dataSrc.getCount());
				if (!refresh) {
					fca.listview.setAdapter(fca.adapter);
				}
				fca.adapter.notifyDataSetChanged();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void rebind() {
		fca.setupAfterUILoad(fca.status, fca.navigation, fca.newPos,
				fca.MODE_NEW, fca.filesInfo);
		fca.navigationBarSetup(fca.newPos, fca.MODE_NEW, fca.navigation);
	}

	public void refresh() {
		try {
			if (fca.adapter == null || fca.listview == null) {
				return;
			}
			Integer pos = fca.adapter.getViewContentMap().get(
					fca.listview.getChildAt(0));
			dataChanged(true);
			fca.setupAfterUILoad(fca.status, fca.navigation, pos == null ? 0
					: pos, fca.MODE_CURR, fca.filesInfo);
			fca.navigationBarSetup(
					fca.navTool.navEntity.peek().defaultPosition,
					fca.MODE_CURR, fca.navigation);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
